define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'note/order/index' + location.search,
                    add_url: 'note/order/add',
                    edit_url: 'note/order/edit',
                    // del_url: 'note/order/del',
                    multi_url: 'note/order/multi',
                    import_url: 'note/order/import',
                    table: 'note_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false, visible: false},
	                    {field: 'contact.name', title: '姓名', operate: 'LIKE'},
	                    {field: 'user.phone', title: '手机号', operate: 'LIKE'},
                        {field: 'customer.name', title: __('客户信息')},
                        {field: 'user.nickname', title: __('用户')},
                        {field: 'order_id', title: __('Order_id'), operate: 'LIKE'},
                        {field: 'service_days', title: __('Service_days')},
                        {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'is_memory', title: __('Is_memory'), searchList: {"0":__('Is_memory 0'),"1":__('Is_memory 1')}, formatter: Table.api.formatter.normal},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate
							,buttons:[
                                {
                                    name: '每日目标',
                                    text: '每日目标',
                                    title:function (row) {
										if(row.user != null) {
                                            return row.user.nickname + '每日目标';
										}else if (row.customer != null){
											return row.customer.name + '每日目标';
										}else{
											return '';
										}
                                    },
                                    icon: '',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'note/day/index?uid={row.uid}&contact_id={row.contact_id}&all=1',
	                                extend: 'data-area=\'["100%","100%"]\''
                                },
                            ]
						}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
				$('[name="row[contact_id]"]').data("params", function (obj) {
					let customer_id = $('[name="row[customer_id]"]').val()
				    return {
						filter: '{"scene_id":"4"}',
					    customer_id:customer_id,
				    };
				});

				/*$('[name="row[customer_id]"]').data('eSelect', function(data){
					let id = data.id;
				});*/
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
